/**
 * 
 */
package org.ejc.struts.registration;

import java.util.Collection;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.ejc.api.payment.PaymentAPIUtil;
import org.ejc.api.registration.RegistrationAPIUtil;
import org.ejc.persist.model.Transaction;
import org.ejc.session.EJCSession;
import org.ejc.tech.combo.ComboConstants;
import org.ejc.tech.combo.ComboMgr;

/**
 * @struts.action path="/struts/register/specialPaymentsAction" scope="request"
 *                parameter="methodToCall" name="specialPaymentsForm"
 *                validate="false"
 *                input="/struts/register/specialPayments.jsp"
 * 
 * @struts.action-forward name="specialPrePaymentSuccess" path="/struts/register/specialPayments.jsp"
 * @struts.action-forward name="sepcialPaymentSuccess" path="/struts/register/specialPaymentsComplete.jsp"
 * 
 * @author Brian Boyle
 */
public class SpecialPaymentsAction extends PaymentAction {

	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public final ActionForward preSpecialPayment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	throws Exception 
	{
		log.debug("In prePayment of SpecialPayment Action!");
		
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Solas Circus Payment");
		
		request.setAttribute("priceTypes",getPriceTypesForSpecialPayments(request.getLocale()));
		
		return mapping.findForward("specialPrePaymentSuccess");
	}
	
	public ActionForward selectSpecialPriceType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	throws Exception 
	{
		log.debug("Selected a Price Type!");
		SpecialPaymentsForm specialPaymentsForm = (SpecialPaymentsForm) form;
		Integer priceTypeId = specialPaymentsForm.getPriceTypeId();
		retrievePrice(request, response, priceTypeId);
		
		return null;
	}
	
	public ActionForward processSpecialPayment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	throws Exception 
	{
		ActionMessages errors = form.validate( mapping, request );
		boolean isConvActive=RegistrationAPIUtil.getLocalHome().create().isConventionActive();
		
		if(!isConvActive){
			if(errors==null)errors=new ActionMessages();
			errors.add("InactiveConv",new ActionMessage("convention.inactive"));
			request.setAttribute("priceTypes",getPriceTypesForSpecialPayments(request.getLocale()));
		}
		
		if ( errors != null && !errors.isEmpty() || isConvActive==false) {
			log.fatal("There are errors");
			saveErrors(request, errors);            
			request.setAttribute("priceTypes",getPriceTypesForSpecialPayments(request.getLocale()));
			return (mapping.findForward("specialPrePaymentSuccess"));
		}
		
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		
		Integer conventionId = ejcSession.getConvention().getConventionId();
		Integer executiveId = ejcSession.getExecutive().getExecutiveId();
		Integer priceId = ejcSession.getCurrentPrice().getPriceId();
		Double priceAmount = ejcSession.getCurrentPrice().getPriceAmount();
		
		Transaction transaction = PaymentAPIUtil.getLocalHome().create().createPayment(conventionId, 1, executiveId, priceId, priceAmount);
		
		request.setAttribute("paidAmount", priceAmount);
		request.setAttribute("changeAmount", 0.0);
		ejcSession.setTransaction(transaction);
		ejcSession.setCurrentAction("Solas Circus Payment Complete");
		
		//Add a Create Sucessful Message
		ActionMessages messages = new ActionMessages();
		ActionMessage msg = new ActionMessage("info.register.createSuccessful");
		messages.add("message1", msg);
		saveMessages(request, messages);
		return mapping.findForward("sepcialPaymentSuccess");
	}
	
	/**
	 * Retrieve a list of the countries
	 * @param locale The user locale
	 * @return List of countries
	 */
	private Collection getPriceTypesForSpecialPayments(Locale locale){
		return ComboMgr.getInstance().getComboContents(ComboConstants.SPECIAL_PRICE_TYPE,locale);
	}

}
